/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 26.07.2008
*/

package it.cums.model;

import it.cums.util.ProductManager;

import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

public class Products {
	private ArrayList<Product> products = new ArrayList<Product>();
	private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
	
	public ArrayList<Product> getProducts() {
		return products;
	}
	
	public void addProduct(Product p) {
		products.add(p);
		p.setPropertyChangeListeners(listeners);
		notifyListeners();
	}
	
	public void removeProduct(Product p) {
		products.remove(p);
		notifyListeners();
	}
	
	public Products() {
		List<Product> productList = ProductManager.getInstance().listProducts();
		if (productList != null) {
			for (Product product : productList) {
				addProduct(product);
			}
		}
	}
	
	private void notifyListeners() {
		for (PropertyChangeListener listener : listeners) {
			listener.propertyChange(null);
		}
	}
	
	public void addChangeListener(PropertyChangeListener listener) {
		listeners.add(listener);
	}
}
